package com.shinbada.modules.sys.web;

import com.shinbada.common.json.AjaxJson;
import com.shinbada.common.utils.StringUtils;
import com.shinbada.core.persistence.Page;
import com.shinbada.core.web.BaseController;
import com.shinbada.exception.ServiceException;
import com.shinbada.modules.sys.entity.Post;
import com.shinbada.modules.sys.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author initcap
 * @date 2020/10/6 13:17
 */
@RestController
@RequestMapping(value = "/sys/post")
@Api(tags = "岗位管理")
public class PostController extends BaseController {

    @Autowired
    private PostService postService;

    @ModelAttribute
    public Post get(@RequestParam(required = false) String id) {
        Post entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = postService.get(id);
        }
        if (entity == null) {
            entity = new Post();
        }
        return entity;
    }

    @RequiresPermissions("user")
    @GetMapping("list")
    @ApiOperation("岗位列表数据")
    public AjaxJson list(Post post, HttpServletRequest request, HttpServletResponse response) {
        Page<Post> page = postService.findPage(new Page<>(request, response), post);
        return AjaxJson.success().put("page", page);
    }

    @RequiresPermissions("user")
    @GetMapping("queryById")
    @ApiOperation("根据Id获取岗位数据")
    public AjaxJson queryById(Post post) {
        return AjaxJson.success().put("post", post);
    }

    @RequiresPermissions(value = {"sys:post:add", "sys:post:edit"}, logical = Logical.OR)
    @PostMapping("save")
    @ApiOperation("保存岗位")
    public AjaxJson save(Post post) {
        /**
         * 后台hibernate-validation插件校验
         */
        String errMsg = beanValidator(post);
        if (StringUtils.isNotBlank(errMsg)) {
            return AjaxJson.error(errMsg);
        }
        //新增或编辑表单保存
        postService.save(post);
        return AjaxJson.success("保存岗位成功");
    }

    @RequiresPermissions("sys:post:del")
    @DeleteMapping("delete")
    @ApiOperation("批量删除岗位")
    public AjaxJson delete(String ids) {
        String[] idArray = ids.split(",");
        StringBuilder errMsg = new StringBuilder();
        int success = 0;
        int fail = 0;
        for (String id : idArray) {
            Post post = postService.get(id);
            try {
                postService.delete(post);
                success++;
            } catch (ServiceException e) {
                errMsg.append("[").append(post.getName()).append("]").append(e.getMessage()).append(";<br/>");
                fail++;
            }
        }
        if (com.shinbada.common.utils.StringUtils.isNotBlank(errMsg.toString())) {
            return AjaxJson.error(errMsg.toString() + "本次批量删除成功:" + success + "条," + "失败: " + fail + "条!");
        }
        return AjaxJson.success("删除岗位成功");
    }

}
